#include <iostream>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *threadRoutine(void *args)
{
    const char *output = static_cast<const char *>(args);

    while (true)
    {
        pthread_mutex_lock(&mutex);

        std::cout << output << std::endl;
        sleep(1);

        //打印完了，就到条件变量中叫醒另外一个线程（第一次是没有线程的），然后自己到条件变量等待
        pthread_cond_signal(&cond);
        pthread_cond_wait(&cond,&mutex);

        pthread_mutex_unlock(&mutex);
    }

    return 0;
}

int main()
{
    pthread_t ta, tb;
    pthread_create(&ta, nullptr, threadRoutine, (void *)"我是线程A");
    pthread_create(&tb, nullptr, threadRoutine, (void *)"我是线程B");

    pthread_join(ta, nullptr);
    pthread_join(tb, nullptr);

    return 0;
}